﻿@model LastoRevival.Service.Contracts.Models.ViewForumModel

@if (Model.Threads.TotalPages == 1)
{
	<!-- no pages to show -->
}

@{
	// normally, we'll show 2 pages before, but only if that doesn't take us less than 1
	// figure out how many before - assume for now we'll go with a max of 2
	var pagesBefore = Math.Min(Model.Threads.CurrentPage - 1, 2);
	
	// now that leaves us with 0, 1 or 2 on the front
	// we want the difference between that and 4, or the number of pages remaining, whatever is less
	var pagesAfter = Math.Min(4 - pagesBefore, Model.Threads.TotalPages - Model.Threads.CurrentPage);
	
	// since we assumed that we couldn't go more than 2 pages in the beginning, we could
	// be at a spot where we have 2 at the front and 0 or 1 at the end
	// if we can add those pages to the front, we will
	if (pagesAfter < 2 && pagesBefore == 2)
	{
		pagesBefore = pagesBefore + (2 - pagesAfter);
	}
	
	// now we either have 4, 3 or 2 on the end
	// that could take us past the end of the pagecount, don't let that happen

	var firstPageToShow = Model.Threads.CurrentPage - pagesBefore;

	if (firstPageToShow == 0)
	{
		firstPageToShow = 1;
	}
	
	// show next 2 pages
	var lastPageToShow = Model.Threads.CurrentPage + pagesAfter;
}

@* // PREVIOUS PAGE LINK *@
@if (Model.Threads.CurrentPage == 1)
{
	// add disabled previous link
	<span class="pageLink">@LanguageLoader.GetWord("PreviousPageLink")</span>
}
else
{
	<a class="pageLink" href="@Url.Action("ViewForumPage", "Forum", new { forumID = Model.ForumDetails.ForumID, page = Model.Threads.CurrentPage - 1 })">@LanguageLoader.GetWord("PreviousPageLink")</a>
}

@* // PAGE NUMBERS *@
@for (var x = firstPageToShow; x <= lastPageToShow; x++)
{
	if (x == Model.Threads.CurrentPage)
	{
		<span class="pageLink currentPage">@x</span>
	}
	else
	{
		<a class="pageLink" href="@Url.Action("ViewForumPage", "Forum", new { forumID = Model.ForumDetails.ForumID, page = x })">@x</a>
	}	
}

@* // PREVIOUS PAGE LINK *@
@if (Model.Threads.CurrentPage == Model.Threads.TotalPages)
{
	// add disabled next link
	<span class="pageLink">@LanguageLoader.GetWord("NextPageLink")</span>
}
else
{
	<a class="pageLink" href="@Url.Action("ViewForumPage", "Forum", new { forumID = Model.ForumDetails.ForumID, page = Model.Threads.CurrentPage + 1 })">@LanguageLoader.GetWord("NextPageLink")</a>
}